﻿using System;
using System.Data.SqlServerCe;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using SenseTimeTracker.Misc;
using SenseTimeTracker.Properties;
using StedySoft.SenseSDK.DrawingCE;

namespace SenseTimeTracker.BusinessLayer
{
	public class User : IColumnSortable
	{
		public const string IdColumnName = "personId";
		public const string NameColumnName = "personName";
		public const string PasswordColumnName = "personPassword";
		public const string PersonNameColumnName = "personUserName";
		public const string TableName = "Persons";

		private Guid id = Guid.NewGuid();
		private string name = string.Empty;
		private string userName = string.Empty;
		private string encryptedPassword = string.Empty;


		public string Type
		{
			get { return string.Empty; }
		}

		public DateTime Date
		{
			get { return DateTime.MinValue; }
		}

		public string Number
		{
			get { return string.Empty; }
		}

		public string Description
		{
			get { return string.Empty; }
		}

		public TimeSpan Duration
		{
			get { return TimeSpan.Zero; }
		}

		public Guid Id
		{
			get { return id; }
			set { id = value; }
		}

		public string Name
		{
			get { return name; }
			set { name = value; }
		}

		public string UserName
		{
			get { return userName; }
			set { userName = value; }
		}

		public string EncryptedPassword
		{
			get { return encryptedPassword; }
			set { encryptedPassword = value; }
		}

		public static List<User> Users
		{
			get
			{
				List<User> users = new List<User>();
				SqlCeCommand command = new SqlCeCommand(
						"SELECT " +
						IdColumnName + ", " +
						NameColumnName + ", " +
						PersonNameColumnName + ", " +
						PasswordColumnName +
						" FROM " + TableName,
						DatabaseManager.DbConnection);
				bool shallOpen = DatabaseManager.DbConnection.State != System.Data.ConnectionState.Open;
				if (shallOpen)
				{
					DatabaseManager.DbConnection.Open();
				}

				SqlCeDataReader reader = command.ExecuteReader();
				while (reader.Read())
				{
					users.Add(new User
					{
						Id = (Guid)reader[IdColumnName],
						Name = reader[NameColumnName].ToString(),
						UserName = reader[PersonNameColumnName].ToString(),
						EncryptedPassword = reader[PasswordColumnName].ToString(),
					});
				}

				if (shallOpen)
				{
					DatabaseManager.DbConnection.Close();
				}

				return users;
			}
		}

		public static User GetUser(Guid userId)
		{
			User user = null;
			SqlCeCommand command = new SqlCeCommand(
					"SELECT " +
					PersonNameColumnName + ", " +
					NameColumnName + ", " +
					PasswordColumnName +
					" FROM " + TableName +
					" WHERE " + IdColumnName + "=@" + IdColumnName,
					DatabaseManager.DbConnection);
			command.Parameters.Add(IdColumnName, userId);
			DatabaseManager.DbConnection.Open();
      SqlCeDataReader reader = command.ExecuteReader();
			if (reader.Read())
			{
				user = new User
				{
					Id = userId,
					Name = reader[NameColumnName].ToString(),
					UserName = reader[PersonNameColumnName].ToString(),
					EncryptedPassword = reader[PasswordColumnName].ToString(),
				};
			}

			DatabaseManager.DbConnection.Close();
			return user;
		}

		public void Delete()
		{
			Delete(Id);
		}

		public static void Delete(Guid personId)
		{
			WorkingTime.Clear(personId);
			SqlCeCommand command = new SqlCeCommand(
				"DELETE FROM " + TableName + " " +
				"WHERE " + IdColumnName + "=@" + IdColumnName,
				DatabaseManager.DbConnection);
			command.Parameters.Add(IdColumnName, personId);
			DatabaseManager.DbConnection.Open();
			try
			{
				command.ExecuteNonQuery();
			}
			catch
			{
				if (SenseAPIs.SenseMessageBox.Show(Resources.DialogMessageRemoveAllTasksAndCards, Resources.DialogHeaderRemoveUser, StedySoft.SenseSDK.SenseMessageBoxButtons.YesNo) == DialogResult.Yes)
				{
					command.CommandText =
						"DELETE FROM " + TimeCard.TableName + " " +
						"WHERE " + TimeCard.PersonIdColumnName + " = @" + IdColumnName;
					command.ExecuteNonQuery();
					command.CommandText =
						"DELETE FROM " + TableName + " " +
						"WHERE " + IdColumnName + "=@" + IdColumnName;
					command.ExecuteNonQuery();
				}
			}

			DatabaseManager.DbConnection.Close();
		}

		public void Insert()
		{
			SqlCeCommand command = new SqlCeCommand(
				"INSERT INTO Persons " +
				"(personId, personName, personPassword) " +
				"VALUES " +
				"(@personId, @personName, @personPassword)",
				DatabaseManager.DbConnection);

			command.Parameters.Add(IdColumnName, id);
			command.Parameters.Add(NameColumnName, name);
			command.Parameters.Add(PasswordColumnName, encryptedPassword);
			DatabaseManager.DbConnection.Open();
			command.ExecuteNonQuery();
			DatabaseManager.DbConnection.Close();
		}

		public void Update()
		{
			SqlCeCommand command = new SqlCeCommand(
        "UPDATE Persons SET " +
        "personName = @personName, " +
        "personPassword = @personPassword " +
        "WHERE personId = @personId",
				DatabaseManager.DbConnection);

			command.Parameters.Add(IdColumnName, id);
			command.Parameters.Add(NameColumnName, name);
			command.Parameters.Add(PasswordColumnName, encryptedPassword);
			DatabaseManager.DbConnection.Open();
			command.ExecuteNonQuery();
			DatabaseManager.DbConnection.Close();
		}
	}
}
